<template>
  <el-form :rules="rules" class="register-container" label-position="left"
           label-width="0px" v-loading="loading">
    <div style="text-align: left; margin-bottom: 15px">
      <el-link type="primary" href="/" :underline=false>返回登录页面</el-link>
    </div>
    <h3 class="register-title">注册</h3>
    <el-form-item prop="email">
      <el-input type="text" v-model="registerForm.email"
                auto-complete="off" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item prop="account">
      <el-input type="text" v-model="registerForm.username"
                auto-complete="off" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item prop="checkPass">
      <el-input type="password" v-model="registerForm.password"
                auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item prop="confirmPass">
      <el-input type="password" v-model="registerForm.confirmPwd"
                auto-complete="off" placeholder="确认密码"></el-input>
    </el-form-item>
    <div style="margin: auto">
      <el-radio class="register-radio" v-model="registerForm.status" label="0">学生</el-radio>
      <el-radio class="register-radio" v-model="registerForm.status" label="1">教师</el-radio>
    </div>
    <el-form-item style="width: 100%">
      <el-button type="primary" style="width: 100%" @click="register">注册</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  export default {
    name: 'Register',
    data() {
      return {
        rules: {
          email: [{required: true, message: '请输入邮箱', trigger: 'blur'}],
          account: [{required: true, message: '请输入用户名', trigger: 'blur'}],
          checkPass: [{required: true, message: '请输入密码', trigger: 'blur'}],
          confirmPass: [{required: true, message: '请再次输入密码', trigger: 'blur'}]
        },
        registerForm: {
          email: '',
          username: '',
          password: '',
          confirmPwd: '',
          status: '0'
        },
        loading: false
      }
    },
    methods: {
      register() {
        var _this = this
        if (this.registerForm.email == null || this.registerForm.email == '') {
          this.$message('邮箱不能为空')
          return false
        }
        if (
          this.registerForm.username == null ||
          this.registerForm.username == ''
        ) {
          this.$message('账号不能为空')
          return false
        }
        if (
          this.registerForm.password == null ||
          this.registerForm.password == '' ||
          this.registerForm.confirmPwd == null ||
          this.registerForm.confirmPwd == ''
        ) {
          this.$message('密码不能为空')
          return false
        }
        if (!(this.registerForm.password === this.registerForm.confirmPwd)) {
          this.$message('两次密码输入不一致')
          return false
        }
        this.loading = true
        this.postRequestByJSON('/register',
          {
            username: this.registerForm.username,
            password: this.registerForm.password,
            email: this.registerForm.email,
            status: this.registerForm.status
          }
        ).then(resp => {
          _this.loading = false
          // console.log(resp)
          var data = resp.data
          if (resp && resp.status == 200) {
            this.$message(data.msg)
            _this.$router.push('/login')
          } else {
            this.$message(data.msg)
          }
        })
      }
    },
    watch: {
      'registerForm.email': function (val) {
        if (val != null && val != '') {
          this.rules.email[0].required = false
        } else {
          this.rules.email[0].required = true
        }
      },
      'registerForm.username': function (val) {
        if (val != null && val != '') {
          this.rules.account[0].required = false
        } else {
          this.rules.account[0].required = true
        }
      },
      'registerForm.password': function (val) {
        if (val != null && val != '') {
          this.rules.checkPass[0].required = false
        } else {
          this.rules.checkPass[0].required = true
        }
      },
      'registerForm.confirmPwd': function (val) {
        if (val != null && val != '') {
          this.rules.confirmPass[0].required = false
        } else {
          this.rules.confirmPass[0].required = true
        }
      }
    }
  }
</script>

<style scoped>
  .register-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin: auto;
    width: 350px;
    padding: 20px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  .register-title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #505458;
  }

  .register-radio {
    margin: 0px 0px 20px 0px;
    text-align: left;
  }
</style>
